package net.mimai.vultrue.bus.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName BatchWorker
 * @Description TODO
 * @Author 方丈
 * @Date 2021/10/26 11:56
 * @Version 1.0.0
 **/
public class BatchWorker<T> {

    final static int batchNum =400;

    public void execute(List<T> taskQueue, Consumer<List<T>> consumer) {
        int batchLimit = (taskQueue.size() + batchNum - 1) / batchNum ;
        List<List<T>> batchList = new ArrayList<>();
        Stream.iterate(0, n -> n + 1).limit(batchLimit).forEach(i -> {
            batchList.add(taskQueue.stream().skip(i * batchNum).limit(batchNum).collect(Collectors.toList()));
        });

        batchList.stream().forEach(
                batch -> consumer.accept(batch)
        );
    }
}
